package demo3;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("张三",10);
        Student student2 = new Student("李四",6);
        Student student3 = new Student("赵五",8);
        Student[] students = {student1,student2,student3};
        AgeComparator agecomparator = new AgeComparator();
        Arrays.sort(students,agecomparator);
        System.out.println(Arrays.toString(students));
    }
    public static void main3(String[] args) {
        Student student1 = new Student("张三",10);
        Student student2 = new Student("李四",6);
        Student student3 = new Student("赵五",8);
        System.out.println("根据年龄比较：");
        AgeComparator agecomparator = new AgeComparator();
        int ret1 = agecomparator.compare(student1,student2);
        if(ret1 > 0) {
            System.out.println("student1 > student2");
        }else if(ret1 == 0) {
            System.out.println("student1 = student2");
        }else {
            System.out.println("student1 < student2");
        }
        System.out.println("根据姓名比较：");
        NameComparator namecomparator = new NameComparator();
        int ret2 = namecomparator.compare(student1,student2);
        if(ret2 > 0) {
            System.out.println("student1 > student2");
        }else if(ret2 == 0) {
            System.out.println("student1 = student2");
        }else {
            System.out.println("student1 < student2");
        }
    }
    //模拟实现Arrays.sort
    public static void bubbleSort(Comparable[] comparable) {
        for (int i = 0; i < comparable.length-1; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if(comparable[j].compareTo(comparable[j+1]) > 0) {
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = tmp;
                }
            }
        }
    }

    public static void main2(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("abao",6);
        Student student3 = new Student("lisi",8);
//        Student[] students = new Student[3];
//        students[0] = student1;
//        students[1] = student2;
//        students[2] = student3;
       Student[] students = {student1,student2,student3};
        //Arrays.sort(students);
        bubbleSort(students);
        System.out.println(Arrays.toString(students));
    }


    public static void main1(String[] args) {
        Student student1 = new Student("张三",10);
        Student student2 = new Student("李四",6);
        Student student3 = new Student("赵五",8);
        int ret = student1.compareTo(student2);
        if(ret > 0) {
            System.out.println("student1 > student2");
        }else if(ret == 0) {
            System.out.println("student1 = student2");
        }else {
            System.out.println("student1 < student2");
        }
    }
}
